package com.cskaoyan.mall.promo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.promo.cache.CacheManager;
import com.cskaoyan.mall.promo.constant.PromoRetCode;
import com.cskaoyan.mall.promo.dto.*;
import com.cskaoyan.mall.promo.form.promo.CreatePromoOrderInfo;
import com.cskaoyan.mall.promo.service.PromoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/shopping")
@Slf4j
public class PromoController {


    @Autowired
    PromoService promoService;


    @Autowired
    CacheManager cacheManager;


    @GetMapping("/seckilllist")
    public ResponseData getPromotionList(@RequestParam Integer sessionId) {
        PromoInfoRequest promoInfoRequest = new PromoInfoRequest();
        promoInfoRequest.setSessionId(sessionId);

        String yyyyMMdd = DateFormatUtils.format(new Date(), "yyyyMMdd");
        promoInfoRequest.setYyyymmdd(yyyyMMdd);
        PromoInfoResponse promoInfoResponse = promoService.getPromoList(promoInfoRequest);
        if (!promoInfoResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(promoInfoResponse.getMsg());
        }
        return new ResponseUtil<>().setData(promoInfoResponse);

    }


    @PostMapping("/promoProductDetail")
    public ResponseData getPromoItemDetail(@RequestBody PromoProductDetailRequest request) {

        PromoProductDetailResponse response = promoService.getPromoProductDetail(request);
        if (!response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil<>().setData(response);
    }


    @PostMapping("/seckill")
    public ResponseData seckill(HttpServletRequest request,
                                @RequestBody CreatePromoOrderInfo createPromoOrderInfo) throws Exception {
        Long productId = createPromoOrderInfo.getProductId();
        Long psId = createPromoOrderInfo.getPsId();

        // 判断秒杀商品是否售罄
        String promoItemStockFlag = "promo_item_stock_not_enough:" + productId + ":" + psId;
        String stockFlag = cacheManager.checkCache(promoItemStockFlag);

        if (stockFlag != null && "none".equals(stockFlag)) {
            // 有库存售罄标志, 直接返回秒杀失败的响应
            return new ResponseUtil<>().setErrorMsg(PromoRetCode.PROMO_ITEM_STOCK_NOT_ENOUGH.getMessage());
        }
        String userInfo =  request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        String username = (String) jsonObject.get("username");
        Integer uid = (Integer) jsonObject.get("uid");
        CreatePromoOrderRequest createPromoOrderRequest = new CreatePromoOrderRequest();
        createPromoOrderRequest.setProductId(productId);
        createPromoOrderRequest.setPsId(psId);
        createPromoOrderRequest.setUserId(uid.longValue());
        createPromoOrderRequest.setUsername(username);

        //增加地址信息
        createPromoOrderRequest.setAddressId(createPromoOrderInfo.getAddressId());
        createPromoOrderRequest.setStreetName(createPromoOrderInfo.getStreetName());
        createPromoOrderRequest.setTel(createPromoOrderInfo.getTel());


        CreatePromoOrderResponse createPromoOrderResponse = promoService.createPromoOrderInTransaction(createPromoOrderRequest);
        ;
        if (!createPromoOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(createPromoOrderResponse.getMsg());
        }

        return new ResponseUtil<>().setData(createPromoOrderResponse);
    }


}
